package edu.scsu.csci.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.scsu.csci.IExecutionErrors;
import edu.scsu.csci.core.ExecutionError;
import edu.scsu.csci.core.ExecutionErrors;


/**
 *
 * @author Maqbool Khan
 * @since jdk 1.5
 * @see
 * @viz.diagram WebUtils.tpx
 */
public final class WebUtils {
	
	/**
	 * holds the error message.
	 */
	private static ThreadLocal<String> error = new ThreadLocal<String>();
	
	/**
	 * holds execution errors
	 */
	public static ThreadLocal<ExecutionErrors> errors = new ThreadLocal<ExecutionErrors>();
	
	/**
	 * holds execution errors
	 */
	public static ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
	
	/**
	 * holds execution errors
	 */
	public static ThreadLocal<HttpServletResponse> response = new ThreadLocal<HttpServletResponse>();
	
	/**
	 * holds execution errors
	 */
	public static ThreadLocal<HttpSession> session = new ThreadLocal<HttpSession>();
	
	
	/**
	 * @return
	 */
	public static HttpSession getSession()	{
		return session.get();
	}
	
	/**
	 * @return
	 */
	public static HttpServletResponse getResponse()	{
		return response.get();
	}
	
	/**
	 * @return
	 */
	public static HttpServletRequest getRequest()	{
		return	request.get();
	}
	
	/**
	 * @param session_p
	 */
	public static void setSession(HttpSession session_p){
		session.set(session_p);
	}
	
	/**
	 * @param request_p
	 */
	public static void setRequest(HttpServletRequest request_p){
		request.set(request_p);
	}
	
	/**
	 * @param response_p
	 */
	public static void setResponse(HttpServletResponse response_p){
		response.set(response_p);
	}
	
	
	/**
	 * Sets the execution error.
	 * 
	 * @param errorMessage
	 * @param fieldName
	 */
	public static void setExecutionError(String fieldName, String errorMessage){
		ExecutionErrors exeErrors = ((ExecutionErrors) errors.get());
		if(exeErrors == null){
			exeErrors = new ExecutionErrors();
		}
		exeErrors.addError(new ExecutionError(fieldName, errorMessage));
		errors.set(exeErrors);
	}
		
	/**
	 * Sets the execution error.
	 * 
	 * @param errorMessage
	 * @param fieldName
	 */
	public static IExecutionErrors getExecutionErrors(){
		return (ExecutionErrors) errors.get();
	}
	
	/**
	 * Sets the error message.
	 * 
	 * @param errorMessage
	 */
	public static void setErrorMessage(String errorMessage){
		error.set(errorMessage);
	}
	
	/**
	 * Returns the error message.
	 * 
	 * @return
	 */
	protected static String getErrorMessage(){
		String errorMessage = error.get();
		error.set(null);
		return errorMessage;
	}
	
}
